<!--
Copyright 2004 ThoughtWorks, Inc

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->
<html>
<head>
<meta content="text/html; charset=Shift_JIS"
http-equiv="content-type">
<title>Selenium Functional Test Runner</title>
<link rel="stylesheet" type="text/css" href="selenium.css" />
<script language="JavaScript" type="text/javascript" src="jsunit/app/jsUnitCore.js"></script>
<script type="text/javascript" src="xmlextras.js"></script>

<script language="JavaScript" type="text/javascript" src="selenium-browserbot.js"></script>
<script language="JavaScript" type="text/javascript" src="selenium-api.js"></script>
<script language="JavaScript" type="text/javascript" src="selenium-commandhandlers.js"></script>
<script language="JavaScript" type="text/javascript" src="selenium-executionloop.js"></script>
<script language="JavaScript" type="text/javascript" src="selenium-tableparser.js"></script>
<script language="JavaScript" type="text/javascript" src="selenium-logging.js"></script>
<script language="JavaScript" type="text/javascript" src="htmlutils.js"></script>
<script language="JavaScript" type="text/javascript" src="xpath.js"></script>
<script language="JavaScript" type="text/javascript" src="user-extensions.js"></script>
<script language="JavaScript" type="text/javascript">
    function openDomViewer() {
        var autFrame = document.getElementById('myiframe');
        var autFrameDocument = getIframeDocument(autFrame);
        var domViewer = window.open('domviewer.html');
        domViewer.rootDocument = autFrameDocument;
        return false;
    }
</script>
<script>
passColor = "#cfffcf";
failColor = "#ffcfcf";
errorColor = "#ffffff";
workingColor = "#DEE7EC";
doneColor = "#FFFFCC";

slowMode = false;

var cmd1 = document.createElement("div");
var cmd2 = document.createElement("div");
var cmd3 = document.createElement("div");
var cmd4 = document.createElement("div");

var postResult = "OK";


function runTest() {
    tableParser = new TableParser();
    var testAppFrame = document.getElementById('myiframe');
    selenium = Selenium.createForFrame(testAppFrame);

    commandFactory = new CommandHandlerFactory();
    commandFactory.registerAll(selenium);

    testLoop = new TestLoop(commandFactory);

    testLoop.firstCommand = startup;
    testLoop.nextCommand = nextCommand;
    testLoop.commandStarted = commandStarted;
    testLoop.commandComplete = commandComplete;
    testLoop.commandError = commandError;
    testLoop.testComplete = function() {window.status = "Selenium Tests Complete, for this Test"};

    testLoop.start();
 }

 function startup() {

    var xmlHttp = XmlHttp.create();
   
    document.getElementById("commandList").appendChild(cmd4);
    document.getElementById("commandList").appendChild(cmd3);
    document.getElementById("commandList").appendChild(cmd2);
    document.getElementById("commandList").appendChild(cmd1);

    try {
        xmlHttp.open("GET", "driver?seleniumStart=true", false);
        xmlHttp.send(null);
    } catch(e) {
        return null;
    }
    return extractCommand(xmlHttp);
 }

 function nextCommand() {

    var xmlHttp = XmlHttp.create();

    try {
        xmlHttp.open("GET", "driver?commandResult=" + postResult, false);
        xmlHttp.send(null);
    } catch(e) {
        return null;
    }
    return extractCommand(xmlHttp);
 }

 function extractCommand(xmlHttp) {

    if (slowMode) {
      delay(2000);
    }
    var command;
    try {
        command = xmlHttp.responseText;
    } catch (e) {
        alert('could not get responseText: ' + e.message);
    }

    
    if (command.substr(0,'|testComplete'.length)=='|testComplete') {
        return null;
    }
    return tableParser.createCommandFromWikiRow(command);
 }

 function commandStarted(command) {
    commandNode = document.createElement("div");
    innerHTML = command.command + '(';
    if (command.target != null && command.target != "") {
        innerHTML += command.target;
        if (command.value != null && command.value != "") {
            innerHTML += ', ' + command.value;
        }
    }
    innerHTML += ")";
    commandNode.innerHTML = innerHTML;
    commandNode.style.backgroundColor = workingColor;
    document.getElementById("commandList").removeChild(cmd1);
    document.getElementById("commandList").removeChild(cmd2);
    document.getElementById("commandList").removeChild(cmd3);
    document.getElementById("commandList").removeChild(cmd4);
    cmd4 = cmd3;
    cmd3 = cmd2;
    cmd2 = cmd1;
    cmd1 = commandNode;
    document.getElementById("commandList").appendChild(cmd4);
    document.getElementById("commandList").appendChild(cmd3);
    document.getElementById("commandList").appendChild(cmd2);
    document.getElementById("commandList").appendChild(cmd1);
 }

 function commandComplete(result) {
    if (result.failed) {
        postResult = result.failureMessage;
        commandNode.title = result.failureMessage;
        commandNode.style.backgroundColor = failColor;
    } else if (result.passed) {
        postResult = "PASSED";
        commandNode.style.backgroundColor = passColor;
    } else {
        postResult = result.result;
        commandNode.style.backgroundColor = doneColor;
    }
 }

 function commandError(message) {
    postResult = "ERROR"
    commandNode.style.backgroundColor = errorColor;
    commandNode.title = message;
 }

 function slowClicked() {
   slowMode = !slowMode;
 }


 function delay(millis) {
   startMillis = new Date();
   while (true) {
     milli = new Date();
     if (milli-startMillis > millis) {
       break;
     }
   }
 }
 
function getIframeDocument(iframe) {
    if (iframe.contentDocument) {
        return iframe.contentDocument;
    }
    else {
        return iframe.contentWindow.document;
    }
}

</script>
</head>

<body onLoad="runTest()">

<table border="1" style="height: 100%;">
  <tr>
    <td width="50%" height="30%">
      <table>
      <tr>
        <td>
          <img src="selenium-logo.png">
        </td>
        <td>
          <h1><a href="http://selenium.thoughtworks.com" >Selenium</a> Functional Testing for Web Apps</h1>
          Open Source From <a href="http://www.thoughtworks.com">ThoughtWorks, Inc</a> and Friends
          <form action="">
          <br/>Slow Mode:<INPUT TYPE="CHECKBOX" NAME="FASTMODE" VALUE="YES" onmouseup="slowClicked()"> 

          <fieldset>
            <legend>Tools</legend>

            <button type="button" id="domViewer1" onclick="openDomViewer();">
              View DOM
            </button>
            <button type="button" onclick="LOG.show();">
              Show Log
            </button>
          </fieldset>

          </form>

        </td>
      </tr>
      </table>
      <form action="">
        <label id="context" name="context"></label>
      </form>
    </td>
    <td width="50%" height="30%">
      <b>Last Four Commands</b><br/>
      <div id="commandList"></div>
    </td>
  </tr>
  <tr>
    <td colspan="2" height="70%">
      <iframe name="myiframe" id="myiframe" src="" height="100%" width="100%"></iframe>
    </td>
  </tr>
</table>

</body>
</html>

